package cn.wolfcode.luowowo.comment.service;

import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IScenicCommentRepository;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {

    @Autowired
    private IScenicCommentRepository scenicCommentRepository;
    @Autowired
    private MongoTemplate template;

    @Override
    public List<ScenicComment> getByUserId(Long uid) {
        return scenicCommentRepository.findByUserId(uid);
    }



    @Override
    public void commentAdd(ScenicComment scenicComment, UserInfo userInfo) {
        scenicComment.setCreateTime(new Date());
        scenicComment.setLevel(userInfo.getLevel());
        scenicComment.setHeadUrl(userInfo.getHeadImgUrl());
        scenicComment.setUsername(userInfo.getNickname());
        //评论的评论
        if (scenicComment.getType()== ScenicComment.SCENIC_COMMENT_TYPE){
            if (StringUtils.hasLength(scenicComment.getRefComment().getId())) {
                Optional<ScenicComment> comment = scenicCommentRepository.findById(scenicComment.getRefComment().getId());
                scenicComment.setRefComment(comment.get());
            }
        }
        //保存
        scenicCommentRepository.save(scenicComment);
    }

    @Override
    public List<ScenicComment> queryTop(Long scenicId) {
        Query query = new Query();
        //条件
        query.addCriteria(Criteria.where("scenicId").is(scenicId));
        //排序
        query.with(Sort.by(Sort.Direction.DESC, "startNum"));
        List<ScenicComment> scenicComments = template.find(query, ScenicComment.class);
        return scenicComments;
    }

    @Override
    public Page queryForComments(ScenicCommentQuery qo) {
        Query query = new Query();
        //总条数
        long count = template.count(query, ScenicComment.class);
        //设置条件
        query.addCriteria(Criteria.where("scenicId").is(qo.getScenicId()));
        if (count==0){
            Page.empty();
        }
        //根据条件判断,点击查询有图美图好评中评差评
        if(qo.getType()<=ScenicCommentQuery.COMMENT_HAS_PICTURE){

        }else if (qo.getType()==ScenicCommentQuery.COMMENT_COMMMIDDLE){
            query.addCriteria(Criteria.where("startNum").is(3));
        }else if(qo.getType()>ScenicCommentQuery.COMMENT_COMMMIDDLE){
            query.addCriteria(Criteria.where("startNum").lt(3));
        }else{
            query.addCriteria(Criteria.where("startNum").gt(3));
        }
        //pageSize
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");
        query.with(pageRequest);
        //点击查询有图
        List<ScenicComment> list = template.find(query, ScenicComment.class);
        if(qo.getType()==ScenicCommentQuery.COMMENT_HAS_PICTURE){
            List<ScenicComment> arr = new ArrayList<>();
            for (ScenicComment scenicComment : list) {
                String pic = scenicComment.getPic();
                if( pic.length()>4){
                    arr.add(scenicComment);
                }
            }
            list = arr;
        }
        return new PageImpl(list,pageRequest,count);
    }

    @Override
    public void commentThumbUp(String id1, Long id2) {
        //根据id1查询出list列表
        ScenicComment comment = template.findById(id1, ScenicComment.class);
        List<Long> list = comment.getThumbuplist();
        int thumbupnum = comment.getThumbupnum();
        //判断当前列表是否有当前用户数据
        //有就减一  无就加一
        if (list.contains(id2)){
            list.remove(id2);
            comment.setThumbupnum(thumbupnum-1);
        }else {
            list.add(id2);
            comment.setThumbupnum(thumbupnum+1);
        }
        template.save(comment);
    }
}
